/**
 * 
 */
	var ws;
	//let ws_url = "ws://localhost:8080/jh/radar";
	let msg;
	var objInc = 0; //异物计数,用于异物ID
	
	/*暂且用于定制，若修改监测点的设备需要改动*/
	var totalFODNums = 0; //异物总数
	var sp1FODNum = 0; //sp1异物总数
	var sp2FODNum = 0; //sp2异物总数
	var sp1Id = '192.168.1.7';
	var sp2Id = '192.168.1.8';
/*	$(function(){
		//ws连接
		alert(loginName);
		ws_connect();
		//前端发送消息给后台
		$("#send").click(function() {
			ws_sendMsg();
		});

	});*/
	function ws_connect(){
		//let loginName = loginName;//'${sessionScope.loginName}';
		console.log('login:ok');
		console.log('loginName:['+loginName+']');
		
        if ('WebSocket' in window) {
            ws = new WebSocket(ws_url+'?loginName='+loginName);
            //ws = new WebSocket(ws_url+'?loginName=admindada');
        } else if ('MozWebSocket' in window) {
            ws = new MozWebSocket(ws_url+'?loginName='+loginName);
        } else {
            console.log('Error: WebSocket is not supported by this browser.');
            return;
        }

        ws.onopen = function () {
            console.log('Info: WebSocket connection opened.');
			//ws_sendMsg();
        };

        ws.onclose = function () {
            console.log('Info: WebSocket closed.');
        };

        ws.onmessage = function (message) {
            console.log(message.data);
    		//前端显示后台返回的信息	

			msg = JSON.parse(message.data);
			var msgType = msg.type;
			console.log("msgType: " + msgType);

			/*系统状态信息处理*/
			if (msgType == 23) {
				console.log('系统状态信息处理');
				console.log(msg.data.ip);
				console.log(msg.data.sysStatus);
				console.log(msg.data.sysFaultCause);
				console.log(msg.data.sysAziAngle);
				console.log(msg.data.sysClutterMap);
				console.log(msg.data.sysRebootNum);	
				let ip = msg.data.ip;	
				let sysStatus = msg.data.sysStatus;	
				let sysFaultCause = msg.data.sysFaultCause;	
				let sysAziAngle = msg.data.sysAziAngle;	
				let sysClutterMap = msg.data.sysClutterMap;	
				let sysRebootNum = msg.data.sysRebootNum;

				let trIndex = ip
				let tdSysStatusIndex = ip+"-b"
				let tdSysFaultCauseIndex = ip+"-c"
				let tdSysAziAngleIndex = ip+"-d"
				let tdSysClutterMapIndex = ip+"-e"
				let tdSysRebootNumIndex = ip+"-f"

				//console.log("----------------01------------------");
				//let value1 = $('#table-sys-tbody').find('tr')[0].id;
				//console.log(value1);

				//let value2 = $($('#table-sys-tbody').find('tr')[0]).attr('radarip');
				//console.log(value2);
				//console.log("----------------0------------------");
				//var trList = $('#table-sys-tbody').find('tr');
				//console.log(trList);

				console.log("----------------1------------------");
				//console.log($("#"+ip+"-1"));
				//console.log($("#a-192.168.1.10"));
				                                   
				//console.log(document.querySelector("#a-192.168.1.10-1"));
				//console.log(document.getElementById("a-192.168.1.10"));


				//let value3 = $($('#table-sys-tbody').find('tr')[0]).text();
				//console.log(value3);
				$('#table-sys-tbody').find('tr').each(function(){
					let trIdValTmp = $(this).attr("id");
					let trIdVal = trIdValTmp + '';
					console.log("trIdVal: " + trIdVal);
					console.log("trIdValTmp: " + trIdValTmp);
					console.log("trIndex: " + trIndex);
					
					//console.log(trIdVal == trIdValTmp);
					//console.log(trIndex == trIdValTmp);
					console.log(trIdVal == trIndex); 
					if(trIndex == trIdVal){
						let time = 1000;
						//let origColor = $(this).css('background'); 
						console.log($(this));
						console.log('-------------------1');
						$(this).addClass("blink-eye-color");
						setTimeout(() =>{
							console.log($(this));
						    $(this).removeClass("blink-eye-color");
						}, time);						

						//setTimeout(function() {
						//	$(this).css("background-color", "#2d93D5");
						//}, time);
						$('#table-sys-tbody').find('tr td').each(function(){
							let tdIdVal = $(this).attr("id");
							if(tdIdVal == tdSysStatusIndex){
								console.log(tdIdVal);
								$(this).html(sysStatus);
							}
							if(tdIdVal == tdSysFaultCauseIndex){
								console.log(tdIdVal);
								$(this).html(sysFaultCause);
								
							}
							if(tdIdVal == tdSysAziAngleIndex){
								console.log(tdIdVal);
								$(this).html(sysAziAngle);
								
							}
							if(tdIdVal == tdSysClutterMapIndex){
								console.log(tdIdVal);
								$(this).html(sysClutterMap);
								
							}
							if(tdIdVal == tdSysRebootNumIndex){
								console.log(tdIdVal);
								$(this).html(sysRebootNum);
								
							}

						});
						return false; 
					}

				});
				/**控制面板显示 */
				if (gCurRadarIP == ip) {
					$("#inputCurRadarID").val(ip);
					$("#inputCurRadarAngle").val(sysAziAngle);
				}
			}

			/*异物信息处理*/
			if (msgType == 24) {
				console.log('异物信息处理');
				console.log(msg.ipcamip);	
				console.log(msg.data.ip);
				console.log(msg.data.objAzi);
				console.log(msg.data.objRange);
				console.log(msg.data.objAmp);
				console.log(msg.data.objAttr);
				console.log(msg.data.objTime);
				
				let ipcamip = msg.ipcamip;
				let ip = msg.data.ip;	
				let objAzi = msg.data.objAzi;	
				let objRange = msg.data.objRange;	
				let objAmp = msg.data.objAmp;	
				let objAttr = msg.data.objAttr;	
				let objTime = msg.data.objTime;

				let trIndex = ip;
				let tdObjIp = ip+"-x";
				let tdObjAziIndex = ip+"-y";
				let tdObjRangeIndex = ip+"-z";
				let tdObjAmpIndex = ip+"-u";
				let tdObjAttrIndex = ip+"-v";
				let tdObjTimeIndex = ip+"-w";
				++objInc;
				console.log('FOD num: ' + objInc);
				objTrHtml = '<tr align="center" class="tb-obj-dev" radarip=' + trIndex + ' ipcamip=' + ipcamip+ ' objAzi=' + objAzi + ' id=' + trIndex + '-' + objInc  +  ' >'  +
						'<td align="center" id=' + tdObjIp +'>' + ip + '</td>'                                  +
						'<td align="center" id=' + tdObjAziIndex +'>' + objAzi + '</td>'                        +
						'<td align="center" id=' + tdObjRangeIndex +'>' + objRange + '</td>'                    +
						'<td align="center" id=' + tdObjAmpIndex +'>' + objAmp + '</td>'                        +
						'<td align="center" id=' + tdObjAttrIndex +'>' + objAttr + '</td>'                      +
						'<td align="center" id=' + tdObjTimeIndex +'>' + objTime + '</td>'                      +					
					
				  	'</tr>';
				$("#table-obj-tbody").append(objTrHtml);

				/*地图展示*/
				switch (trIndex){
					case sp1Id:
						console.log('地图展示1 trIndex: ' + trIndex);
						if(!($('#sp1').hasClass("danger"))){
							console.log('地图展示1 hasClass: ' + $('#sp1').hasClass("danger"));
							$('#sp1').removeClass();
							$('#sp1').addClass("danger");
						}
						sp1FODNum++;
						$('#sp1FODNumId').text(sp1FODNum);
						console.log('地图展示1 sp1FODNum: ' + sp1FODNum);
						break;
					case sp2Id:
						if(!($('#sp2').hasClass("danger"))){
							$('#sp2').removeClass();
							$('#sp2').addClass("danger");
						}
						sp2FODNum++;
						$('#sp2FODNumId').text(sp2FODNum);
						
						break;
					default:
						break;
					
				}
				
				
/*				console.log("----------------o1------------------");
				$('#table-obj-tbody').find('tr').each(function(){
					let trIdValTmp = $(this).attr("radarip");
					let trIdVal = trIdValTmp + '';
					console.log("trIdVal: " + trIdVal);
					console.log("trIdValTmp: " + trIdValTmp);
					console.log("trIndex: " + trIndex);
					
					//console.log(trIdVal == trIdValTmp);
					//console.log(trIndex == trIdValTmp);
					console.log(trIdVal == trIndex); 
					if(trIndex == trIdVal){
						let time = 1000;
						//let origColor = $(this).css('background'); 
						//console.log("origColor:  " + origColor);
						console.log($(this));
						console.log('-------------------o1');
						//$(this).css('background', '#9BC1E5');
						$(this).addClass("blink-eye-color");
						setTimeout(() =>{
							//$(this).css('background-color', 'green');
							console.log($(this));
							//$(this).css("background-color", origColor);
						    $(this).removeClass("blink-eye-color");
						}, time);						

						$('#table-obj-tbody').find('tr td').each(function(){
							let tdIdVal = $(this).attr("id");
							if(tdIdVal == tdObjAziIndex){
								console.log(tdIdVal);
								$(this).html(objAzi);
							}
							if(tdIdVal == tdObjRangeIndex){
								console.log(tdIdVal);
								$(this).html(objRange);
								
							}
							if(tdIdVal == tdObjAmpIndex){
								console.log(tdIdVal);
								$(this).html(objAmp);
								
							}
							if(tdIdVal == tdObjAttrIndex){
								console.log(tdIdVal);
								$(this).html(objAttr);
								
							}
							if(tdIdVal == tdObjTimeIndex){
								console.log(tdIdVal);
								$(this).html(objTime);
								
							}

						});
						return false; 
					}

				}); */

			}

			/*门限反馈信息处理*/
			if (msgType == 21) {
				console.log('门限反馈信息处理');
				console.log(msg.data.ip);
				console.log(msg.data.error);	
				console.log(msg.data.threshold);
				let ip = msg.data.ip;	
				let error = msg.data.error;
				let threshold = msg.data.threshold;

				console.log("----------------21-1------------------");
				/**控制面板显示 */
				if (gCurRadarIP == ip) {
					if (error == 0) {
						$("#inputThreshVal").val(threshold);
						console.log('门限反馈信息正确：' + error);
					}else{
						console.log('门限反馈信息错误：' + error);
					}
					
				}

			}			

			/*补光灯反馈信息处理*/
			if (msgType == 10) {
				console.log('补光灯反馈信息处理');
				console.log(msg.data.ip);
				console.log(msg.data.error);	
				console.log(msg.data.ledCmd);	
				console.log(msg.data.ledCmdStatus);
				let ip = msg.data.ip;	
				let error = msg.data.error;
				let ledCmd = msg.data.ledCmd;
				let ledCmdStatus = msg.data.ledCmdStatus;
			
				console.log("----------------10-1------------------");
				/**控制面板显示 */
				if (gCurRadarIP == ip) {
					if (error == 0) {
						if (ledCmd == 0) {
							$("#btnAutoCtrlLedText").html('自动禁止'); 
						} else if (ledCmd == 2){
							$("#btnAutoCtrlLedText").html('自动控制'); 
						}
	
						if (ledCmd == 1) {
							$("#btnHandCtrlLedText").html('手动禁止'); 
						} else if (ledCmd == 3){
							$("#btnHandCtrlLedText").html('手动控制'); 
						}	
					}
					else{
						console.log('补光灯反馈信息错误：' + error);
						alert('补光灯反馈信息错误：' + error);
					}
				}
			
			}
			/*单点驻留*/
			if (msgType == 12) {
				console.log('单点驻留反馈信息处理');
				console.log(msg.data.ip);
				console.log(msg.data.error);	
				console.log(msg.data.speed);	
				console.log(msg.data.posAngle);
				console.log(msg.data.swiveResideStatus);
				let ip = msg.data.ip;	
				let error = msg.data.error;
				let speed = msg.data.speed;
				let posAngle = msg.data.posAngle;
				let swiveResideStatus = msg.data.swiveResideStatus;
			
				console.log("----------------12-1------------------");
				/**控制面板显示 */
				if (gCurRadarIP == ip) {
					if (error == 0) {
						$("#swiveResideSpeed").val(speed);
						$("#swiveResidePos").val(posAngle)
					}else{
						console.log('单点驻留反馈信息错误：' + error);
						alert('单点驻留反馈信息错误：' + error);	
					}
				}
			
			}
			/*手动操作*/
			if (msgType == 11) {
				console.log('手动操作反馈信息处理');
				console.log(msg.data.ip);
				console.log(msg.data.error);	
				console.log(msg.data.speed);	
				console.log(msg.data.direc);
				console.log(msg.data.swiveControlStatus);
				let ip = msg.data.ip;	
				let error = msg.data.error;
				let speed = msg.data.speed;
				let direc = msg.data.direc;
				let swiveControlStatus = msg.data.swiveControlStatus;
			
				console.log("----------------11-1------------------");
				/**控制面板显示 */
				if (gCurRadarIP == ip) {
					if (error == 0) {
						$("#swiveSpeed").val(speed);
						if (direc == 1)
						{
							//$("#selectSwiveDirec")[0].selectedIndex = 2; //由选项中的排序序号（0-2），来选中
							$("#swiveDirec").val(1);
						}
						else if (direc == -1)
						{
							$("#swiveDirec").val(-1);
						}
						else
						{
							$("#swiveDirec").val(0);
						}  
					}else{
						console.log('手动操作反馈信息错误：' + error);
						alert('手动操作反馈信息错误：' + error);	
					}
				}
			
			}			
			/** */
			/** */


        };	
		//ws_sendMsg();	
	};
	function ws_sendMsg(){
		let sendMsgContent = "hello,from ws server Msg";
		ws.send(sendMsgContent);
	};
	function ws_send(msg){
		if (null == msg || 'undefined' == typeof(msg) || '' == msg) {
			return false;
		}
		ws.send(msg);
	};
	function clearFODMap(){
		if(sp1FODNum > 0 ){
			$('#sp1').removeClass();
			$('#sp1').addClass("safe");
			sp1FODNum = 0;
			$('#sp1FODNumId').text('');	
		}
		if(sp2FODNum > 0 ){
			$('#sp2').removeClass();
			$('#sp2').addClass("safe");
			sp2FODNum = 0;
			$('#sp2FODNumId').text('');	
		}

	}